package com.tencent.sr.iris.activity.service.event.handler;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.enums.ActivityTypeEnum;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.service.activity.IrisActivityService;
import com.tencent.sr.iris.activity.service.event.activity.IrisActivityExecutor;
import com.tencent.sr.iris.activity.service.event.activity.UserLevelUpFosterActivityExecutor;
import com.tencent.sr.iris.activity.service.event.activity.UserLevelUpPromoActivityExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户升级事件处理
 *
 * @author wruiiwang
 * @date 2023/2/17
 */
@Slf4j
@Component("LEVEL_UP")
public class UserLevelUpHandler extends AbstractIrisEventHandler {

    @Resource
    private UserLevelUpFosterActivityExecutor userLevelUpFosterActivityExecutor;
    @Resource
    private UserLevelUpPromoActivityExecutor userLevelUpPromoActivityExecutor;


    @Resource
    private IrisActivityService activityService;


    @Override
    public void after(ActivityEventDTO eventDTO) {

    }

    @Override
    void process(ActivityEventDTO eventDTO, List<String> eventHandingInfoList) {
        //补充pid
        addParameterForEvent(eventDTO);
        List<ActivityDTO> activityList = getActivity(eventDTO);
        if (CollectionUtil.isEmpty(activityList)) {
            log.info("处理成功,事件对应的活动列表为空，忽略事件,event:{}", JSON.toJSONString(eventDTO));
            eventHandingInfoList
                    .add(this.getClass().getSimpleName() + "处理成功,事件对应的活动列表为空，忽略事件:" + eventDTO.getIdempotent());
            return;
        }

        activityList.stream().forEach(activity -> {
            IrisActivityExecutor activityExecutor = getActivityExecutor(eventDTO, activity);
            if (activityExecutor == null) {
                log.info(this.getClass().getSimpleName() + "处理成功,没有找到活动对应的处理器，忽略事件,event:{}",
                        JSON.toJSONString(eventDTO));
                eventHandingInfoList
                        .add(this.getClass().getSimpleName() + "处理成功,没有找到活动对应的处理器，忽略事件:" + eventDTO.getIdempotent());
                return;
            }
            activityExecutor.execute(eventDTO, activity);
        });
        eventHandingInfoList.add(this.getClass().getSimpleName() + "处理成功,idempotent:" + eventDTO.getIdempotent());
    }


    @Override
    List<ActivityTaskDTO> getRelatedTaskList(String eventType) {
        return null;
    }

    @Override
    void baseDataProcess(ActivityEventDTO eventDTO) {

    }


    protected List<ActivityDTO> getActivity(ActivityEventDTO msg) {
        List<ActivityDTO> list = Lists.newArrayList();
        // 查询活动信息
        ActivityDTO activityFoster = activityService.getActivity(activityConfig.getLevelUpFosterActivityId());
        if (activityFoster != null) {
            list.add(activityFoster);
        }

        // 查询活动信息
        ActivityDTO activityPromo = activityService.getActivity(activityConfig.getLevelUpPromoActivityId());
        if (activityPromo != null) {
            list.add(activityPromo);
        }

        // 查询月月领活动
        ActivityDTO activityDTO = activityService.getActivity(activityConfig.getMonthGiftActivityId());
        if (activityDTO != null) {
            list.add(activityDTO);
        }

        // 查询辅导津贴活动信息
        ActivityDTO guideAllowanceActivityId = activityService.getActivity(activityConfig.getGuideAllowanceActivityId());
        if (guideAllowanceActivityId != null) {
            list.add(guideAllowanceActivityId);
        }

        return list;
    }

}
